<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
	Project		: SUMMON
	Developer	: APPropolis Inc.
	Programmer	: Mark Vincent M. Lorenzo
	(c) 2014
*/

class Dashboard extends CI_Controller {

	public function __construct() {
		parent::__construct();
		//$this->output->cache(60);
		ini_set('session.cookie_httponly',1); 
		ini_set('session.use_only_cookies',1);
		$this->load->library('Commons');
		$this->load->model('auth_class');
		$this->load->model('users_class');
		$this->load->model('message_class');
		$this->load->model('restaurant_class');
		$this->load->model('reservation_class');
	}	
	public function global_data()
	{
		$this->auth_class->no_session();
		$strEmail = $this->session->userdata('strEmail');
		$getUserInfo = $this->users_class->get_user_info($strEmail);
		$data['intUserID'] = $getUserInfo['intUserID'];
		if($getUserInfo!=0)
		{
			$data['myID'] = $getUserInfo['intUserID'];
			// Determine if the middle name have a content or none
			if($getUserInfo['strMiddleName']!='')
			{
				$midName = $getUserInfo['strMiddleName'].' ';
			}
			else
			{
				$midName = '';
			}
			// Get Account Type
			if($getUserInfo['intAccountType']=='0')
			{
				$data['userType'] = 'Client User';
			}
			else if($getUserInfo['intAccountType']=='1')
			{
				$data['userType'] = 'Administrator';
			}
			else
			{
				$data['userType'] = 'Restaurant Owner';
			}
			$data['fullname'] = $getUserInfo['strFirstName'].' '.$midName.''.$getUserInfo['strLastName'];
		}
		else
		{
			$data['fullname'] = '';
		}
		return $data;
	}
	public function index()
	{
		$data = $this->global_data();
		
		$this->load->view('commons/header', $data);
		$this->load->view('dashboard/commons/css');
		$this->load->view('dashboard/index');
		$this->load->view('commons/js');
		$this->load->view('dashboard/commons/js');
		$this->load->view('commons/footer');
	}
	public function account_settings($id=null)
	{
		$data = $this->global_data();
		
		// Get User Information
		if($id!='')
		{
			$data['userID'] = $id;
		}
		else
		{
			$data['userID'] = $data['intUserID'];
		}
		$data['intUserID'] = $id;
		$getUserData = $this->users_class->get_user_data($data);
		if($getUserData!=0)
		{
			$data['myID'] = $data['intUserID'];
			// Determine if the middle name have a content or none
			if($getUserData['strMiddleName']!='')
			{
				$midName = $getUserData['strMiddleName'].' ';
			}
			else
			{
				$midName = '';
			}
			// Get Account Type
			if($getUserData['intAccountType']=='0')
			{
				$data['userRole'] = 'Client User';
			}
			else if($getUserData['intAccountType']=='1')
			{
				$data['userRole'] = 'Administrator';
			}
			else
			{
				$data['userRole'] = 'Restaurant Owner';
			}
			$data['strUserFullName'] = $getUserData['strFirstName'].' '.$midName.''.$getUserData['strLastName'];
			$data['strUserEmail'] = $getUserData['strEmail'];
			$data['strUserGender'] = $getUserData['strGender'];
			$data['strUserCivilStatus'] = $getUserData['strCivilStatus'];
			$data['birthUserMonth'] = $getUserData['birthMonth'];
			$data['birthUserDay'] = $getUserData['birthDay'];
			$data['birthUserYear'] = $getUserData['birthYear'];
			$data['strUserAddress'] = $getUserData['strAddress'];
			$data['strUserCity'] = $getUserData['strCity'];
			$data['strUserHometown'] = $getUserData['strHometown'];
			$data['strUserProvince'] = $getUserData['strProvince'];
			$data['strUserCountry'] = $getUserData['strCountry'];
			$data['strUserMobile'] = $getUserData['strMobile'];
			$data['strUserLandline'] = $getUserData['strLandline'];
			$data['pageNotFound'] = 1;
		}
		else
		{
			$data['pageNotFound'] = 0;
		}
		
		$this->load->view('commons/header', $data);
		$this->load->view('account_settings/commons/css');
		$this->load->view('account_settings/index');
		$this->load->view('commons/js');
		$this->load->view('account_settings/commons/js');
		$this->load->view('commons/footer');
	}
	public function update_profile($id=null)
	{
		$data = $this->global_data();
		
		// Get User Information
		if($id!='')
		{
			$data['userID'] = $id;
		}
		else
		{
			$data['userID'] = $data['intUserID'];
		}
		$data['intUserID'] = $id;
		$getUserData = $this->users_class->get_user_data($data);
		if($getUserData!=0)
		{
			// Determine if the middle name have a content or none
			if($getUserData['strMiddleName']!='')
			{
				$midName = $getUserData['strMiddleName'];
			}
			else
			{
				$midName = '';
			}
			$data['strUserFullName'] = $getUserData['strFirstName'].' '.$midName.''.$getUserData['strLastName'];
			$data['strEditFirstName'] = $getUserData['strFirstName'];
			$data['strEditMidName'] = $midName;
			$data['strEditLastName'] = $getUserData['strLastName'];
			$data['strEditUserEmail'] = $getUserData['strEmail'];
			$data['strEditUserPassword'] = $this->encrypt->decode($getUserData['strPassword']);
			$data['intEditUserType'] = $getUserData['intAccountType'];
			$data['strEditUserGender'] = $getUserData['strGender'];
			$data['strEditUserCivilStatus'] = $getUserData['strCivilStatus'];
			$data['strEditUserBirthMonth'] = $getUserData['birthMonth'];
			$data['strEditUserBirthDay'] = $getUserData['birthDay'];
			$data['strEditUserBirthYear'] = $getUserData['birthYear'];
			$data['strEditUserAddress'] = $getUserData['strAddress'];
			$data['strEditUserCity'] = $getUserData['strCity'];
			$data['strEditUserHometown'] = $getUserData['strHometown'];
			$data['strEditUserProvince'] = $getUserData['strProvince'];
			$data['strEditUserCountry'] = $getUserData['strCountry'];
			$data['strEditUserMobile'] = $getUserData['strMobile'];
			$data['strEditUserLandline'] = $getUserData['strLandline'];
			$data['pageNotFound'] = 1;
		}
		else
		{
			$data['pageNotFound'] = 0;
		}
		
		$post = $this->input->post();
		if($post['doUpdate']=='update')
		{
			$processUpdate = $this->update($post,$id);
			$data['strMsg'] = $processUpdate['intStatus'];
		}
		else
		{
			$data['strMsg'] = '';
		}
		
		$this->load->view('commons/header', $data);
		$this->load->view('update_profile/commons/css');
		$this->load->view('update_profile/index');
		$this->load->view('commons/js');
		$this->load->view('update_profile/commons/js');
		$this->load->view('commons/footer');
	}
	public function update($post,$id)
	{
		$updateUser = $this->users_class->update($post,$id);
		return $updateUser;
	}
	public function messages()
	{
		$data = $this->global_data();
		
		// List Messages
		$getUserMsg = $this->message_class->list_msg();
		if($getUserMsg!=0)
		{
			$data['getUserMsg'] = $getUserMsg;
		}
		else
		{
			$data['getUserMsg'] = 0; // No message
		}
		
		$this->load->view('commons/header', $data);
		$this->load->view('messages/commons/css');
		$this->load->view('messages/index');
		$this->load->view('commons/js');
		$this->load->view('messages/commons/js');
		$this->load->view('commons/footer');	
	}
	public function get_message()
	{
		$post = $this->input->post();
		
		$getMsg = $this->message_class->get_message($post);
		if($getMsg!=0)
		{
			$data['strFirstName'] = $getMsg['strFirstName'];
			$data['strMiddleName'] = $getMsg['strMiddleName'];
			$data['strLastName'] = $getMsg['strLastName'];
			$data['strFrom'] = $getMsg['strFrom'];
			$data['strMsgBody'] = $getMsg['strMsgBody'];
			$data['intStatus'] = $getMsg['intStatus'];
			if($getMsg['intAccountType']==0)
			{
				$data['strRank'] = 'Client';
			}
			else if($getMsg['intAccountType']==1)
			{
				$data['strRank'] = 'Administrator';
			}
			else
			{
				$data['strRank'] = 'Restaurant Owner';
			}
			$data['dateCreated'] = date("F t, Y g:ia", strtotime($getMsg['dateCreated']));
		}
		
		echo json_encode($data);	
	}
	public function get_msg_status()
	{
		$post = $this->input->post();
		
		$getStatus = $this->message_class->get_message($post);
		if($getStatus!=0)
		{
			$data['intStatus'] = $getStatus['intStatus'];
		}
		
		echo json_encode($data);	
	}
	public function update_msg_status()
	{
		$post = $this->input->post();
		
		$updateMsgStatus = $this->message_class->update_msg_status($post);
		
		echo json_encode($updateMsgStatus);	
	}
	public function count_new_msg()
	{
		$countNewMsg = $this->message_class->count_new_msg();
		echo json_encode($countNewMsg);	
	}
	public function compose()
	{
		$data = $this->global_data();
		
		$this->load->view('commons/header', $data);
		$this->load->view('compose/commons/css');
		$this->load->view('compose/index');
		$this->load->view('commons/js');
		$this->load->view('compose/commons/js');
		$this->load->view('commons/footer');
	}
	public function get_recipient()
	{
		$post = $this->input->post();
		
		$getRecipient = $this->users_class->get_recipient($post);
		foreach($getRecipient as $key=>$val)
		{
			$data['intUserID'][$key] = $val['intUserID'];
			$data['strFirstName'][$key] = $val['strFirstName'];
			$data['strMiddleName'][$key] = $val['strMiddleName'];
			$data['strLastName'][$key] = $val['strLastName'];
			$data['strEmail'][$key] = $val['strEmail'];
			// Get Account Type
			if($val['intAccountType']=='0')
			{
				$data['userType'][$key] = 'Client User';
			}
			else if($val['intAccountType']=='1')
			{
				$data['userType'][$key] = 'Administrator';
			}
			else
			{
				$data['userType'][$key] = 'Restaurant Owner';
			}
		}
		echo json_encode($data);
	}
	public function send_message()
	{
		$post = $this->input->post();
		
		$sendMsg = $this->message_class->send_message($post);
		
		echo json_encode($sendMsg);
	}	
	public function delete_msg()
	{
		$post = $this->input->post();
		
		$delMsg = $this->message_class->delete_msg($post);
		
		echo json_encode($delMsg);
	}
	public function sent_items()
	{
		$data = $this->global_data();
		
		// List Messages
		$getUserMsg = $this->message_class->list_sent_msg();
		if($getUserMsg!=0)
		{
			$data['getUserMsg'] = $getUserMsg;
		}
		else
		{
			$data['getUserMsg'] = 0; // No message
		}
		
		$this->load->view('commons/header', $data);
		$this->load->view('sent_items/commons/css');
		$this->load->view('sent_items/index');
		$this->load->view('commons/js');
		$this->load->view('sent_items/commons/js');
		$this->load->view('commons/footer');
	}
	public function get_sent_message()
	{
		$post = $this->input->post();
		
		$getMsg = $this->message_class->get_sent_message($post);
		if($getMsg!=0)
		{
			$data['strFirstName'] = $getMsg['strFirstName'];
			$data['strMiddleName'] = $getMsg['strMiddleName'];
			$data['strLastName'] = $getMsg['strLastName'];
			$data['strFrom'] = $getMsg['strFrom'];
			$data['strMsgBody'] = $getMsg['strMsgBody'];
			$data['intStatus'] = $getMsg['intStatus'];
			if($getMsg['intAccountType']==0)
			{
				$data['strRank'] = 'Client';
			}
			else if($getMsg['intAccountType']==1)
			{
				$data['strRank'] = 'Administrator';
			}
			else
			{
				$data['strRank'] = 'Restaurant Owner';
			}
			$data['dateCreated'] = date("F t, Y g:ia", strtotime($getMsg['dateCreated']));
		}
		
		echo json_encode($data);	
	}
	public function delete_sent_msg()
	{
		$post = $this->input->post();
		
		$delMsg = $this->message_class->delete_sent_msg($post);
		
		echo json_encode($delMsg);
	}
	public function message_trash()
	{
		$data = $this->global_data();
		
		// List Messages
		$getUserMsg = $this->message_class->message_trash();
		if($getUserMsg!=0)
		{
			$data['getUserMsg'] = $getUserMsg;
		}
		else
		{
			$data['getUserMsg'] = 0; // No message
		}
		
		$this->load->view('commons/header', $data);
		$this->load->view('message_trash/commons/css');
		$this->load->view('message_trash/index');
		$this->load->view('commons/js');
		$this->load->view('message_trash/commons/js');
		$this->load->view('commons/footer');
	}
	public function get_trash_message()
	{
		$post = $this->input->post();
		
		$getMsg = $this->message_class->get_trash_message($post);
		if($getMsg!=0)
		{
			$data['strFirstName'] = $getMsg['strFirstName'];
			$data['strMiddleName'] = $getMsg['strMiddleName'];
			$data['strLastName'] = $getMsg['strLastName'];
			$data['strFrom'] = $getMsg['strFrom'];
			$data['strMsgBody'] = $getMsg['strMsgBody'];
			$data['intStatus'] = $getMsg['intStatus'];
			if($getMsg['intAccountType']==0)
			{
				$data['strRank'] = 'Client';
			}
			else if($getMsg['intAccountType']==1)
			{
				$data['strRank'] = 'Administrator';
			}
			else
			{
				$data['strRank'] = 'Restaurant Owner';
			}
			$data['dateCreated'] = date("F t, Y g:ia", strtotime($getMsg['dateCreated']));
		}
		
		echo json_encode($data);	
	}
	public function delete_trash_msg()
	{
		$post = $this->input->post();
		
		$delMsg = $this->message_class->delete_trash_msg($post);
		
		echo json_encode($delMsg);
	}
	public function restaurants($page=null)
	{
		$data = $this->global_data();
		
		if (!$page) {
			$offset = 0;
		} else {
			$offset = $page;
		}
		if($this->session->userdata('intAccountType')!='1')
		{
			$data['listBranches'] = $this->restaurant_class->list_branch_resto($offset, $this->commons->MAX_RESULTS);
			$totalPage = $this->restaurant_class->get_branch_total();
		}
		else
		{
			$data['listMainBranch'] = $this->restaurant_class->list_main_branch($offset, $this->commons->MAX_RESULTS);
			$totalPage = $this->restaurant_class->list_main_branch_total();
		}
		
		$pageConfig = $this->commons->createPagingConfig(base_url().'dashboard/restaurants', $totalPage, $this->commons->MAX_RESULTS);
		$this->pagination->initialize($pageConfig);
		$pageLinks = $this->pagination->create_links();
		
		$data['totalPage'] = $totalPage;
		$data['pageLinks'] = $pageLinks;
		

		$data['getMainBranch'] = $this->restaurant_class->get_main_branch();
		$data['mainBranchExist'] = $this->restaurant_class->main_branch_exist();
		$data['isBranchOwner'] = $this->restaurant_class->is_branch_owner();
		$data['getMyBranch'] = $this->restaurant_class->get_my_branch();

		
		$this->load->view('commons/header', $data);
		$this->load->view('restaurants/commons/css');
		$this->load->view('restaurants/index');
		$this->load->view('commons/js');
		$this->load->view('restaurants/commons/js');
		$this->load->view('commons/footer');
	}
	public function add_owner()
	{
		$data = $this->global_data();
		
		$post = $this->input->post();
		if(isset($post['doAddOwner']))
		{
			if($post['doAddOwner']=='add_owner')
			{
				$saveOwnerDetails = $this->save_resto_owner($post);
				if($saveOwnerDetails)
				{
					$data['intStatus'] = 1;
				}
				else
				{
					$data['intStatus'] = 0;
				}
			}
		}
		else
		{
			$data['intStatus'] = '';
		}
		
		$this->load->view('commons/header', $data);
		$this->load->view('add_owner/commons/css');
		$this->load->view('add_owner/index');
		$this->load->view('commons/js');
		$this->load->view('add_owner/commons/js');
		$this->load->view('commons/footer');
	}
	public function add_restaurant()
	{
		$data = $this->global_data();
		
		$post = $this->input->post();
		if(isset($post['doAddResto']))
		{
			if($post['doAddResto']=='add_resto')
			{
				$saveRestoDetails = $this->save_resto_details($post);
				if($saveRestoDetails['picMsg'] == 'err_thumbnail')
				{
					$data['picMsg'] = 'err_thumbnail';
				}
				else if($saveRestoDetails['picMsg'] == 'err_banner')
				{
					$data['picMsg'] = 'err_banner';
				}
				else if($saveRestoDetails)
				{
					$data['intStatus'] = 1;
					$data['picMsg'] = '';
				}
				else
				{
					$data['intStatus'] = 0;
					$data['picMsg'] = '';
				}
			}
		}
		else
		{
			$data['intStatus'] = '';
			$data['picMsg'] = '';
		}
		
		$data['listRestoOwner'] = $this->users_class->resto_owner_dropdown();
		$data['mainBranchExist'] = $this->restaurant_class->main_branch_exist();
		$data['isBranchOwner'] = $this->restaurant_class->is_branch_owner();
		
		$this->load->view('commons/header', $data);
		$this->load->view('add_restaurant/commons/css');
		$this->load->view('add_restaurant/index');
		$this->load->view('commons/js');
		$this->load->view('add_restaurant/commons/js');
		$this->load->view('commons/footer');
	}
	public function save_resto_owner($post)
	{
		$restoOwner = $this->users_class->save_resto_owner($post);
		
		return $restoOwner;
	}
	public function save_resto_details()
	{
		$post = $this->input->post();

		$imgRestoThumbSize = getimagesize($_FILES['strRestoThumbnail']['tmp_name'][0]);
		$imgRestoBannerSize = getimagesize($_FILES['strRestoBanner']['tmp_name'][0]);

		if($imgRestoThumbSize[0]!='189' || $imgRestoThumbSize[1]!='86')
		{
			$saveRestoDetails['picMsg'] = 'err_thumbnail';
		}
		else if($imgRestoBannerSize[0]!='1098' || $imgRestoBannerSize[1]!='469')
		{
			$saveRestoDetails['picMsg'] = 'err_banner';
		}
		else
		{
			$saveRestoDetails = $this->restaurant_class->save_resto_details($post);
			
			if($saveRestoDetails['intStatus']=='1')
			{
				$valid_formats = array("jpg", "jpeg", "png");
				$max_file_size = 1024*20480; //20 MB
				$randNumber = rand('111111','999999');
				
				if($_FILES["strRestoThumbnail"]["tmp_name"]!='')
				{
					$path = "assets/uploaded/thumbnails/"; // Upload directory
					$count = 0;
					
					if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
						// Loop $_FILES to execute all files
						foreach ($_FILES['strRestoThumbnail']['name'] as $f => $name) {     
							if ($_FILES['strRestoThumbnail']['error'][$f] == 4) {
								continue; // Skip file if any error found
							}	       
							if ($_FILES['strRestoThumbnail']['error'][$f] == 0) {	           
								if ($_FILES['strRestoThumbnail']['size'][$f] > $max_file_size) {
									$message[] = "$name is too large!.";
									continue; // Skip large files
								}
								elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
									$message[] = "$name is not a valid format";
									continue; // Skip invalid file formats
								}
								else{ // No error found! Move uploaded files 
									$fileName = $randNumber.'_'.$name; 
									if(move_uploaded_file($_FILES["strRestoThumbnail"]["tmp_name"][$f], $path.$fileName))
									$intRestoID = stripslashes(filter_var($post['intRestoOwer'], FILTER_SANITIZE_STRING));
									$strRestoThumb = $fileName;
									$this->restaurant_class->save_resto_thumb($intRestoID, $strRestoThumb);
									$count++; // Number of successfully uploaded file
								}
							}
						}
					}
				}
				if($_FILES["strRestoBanner"]["tmp_name"]!='')
				{
					$path = "assets/uploaded/banners/"; // Upload directory
					$count = 0;
					
					if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
						// Loop $_FILES to execute all files
						foreach ($_FILES['strRestoBanner']['name'] as $f => $name) {     
							if ($_FILES['strRestoBanner']['error'][$f] == 4) {
								continue; // Skip file if any error found
							}	       
							if ($_FILES['strRestoBanner']['error'][$f] == 0) {	           
								if ($_FILES['strRestoBanner']['size'][$f] > $max_file_size) {
									$message[] = "$name is too large!.";
									continue; // Skip large files
								}
								elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
									$message[] = "$name is not a valid format";
									continue; // Skip invalid file formats
								}
								else{ // No error found! Move uploaded files 
									$fileName = $randNumber.'_'.$name; 
									if(move_uploaded_file($_FILES["strRestoBanner"]["tmp_name"][$f], $path.$fileName))
									$intRestoID = stripslashes(filter_var($post['intRestoOwer'], FILTER_SANITIZE_STRING));
									$strRestoBanner = $fileName;
									$this->restaurant_class->save_resto_banner($intRestoID, $strRestoBanner);
									$count++; // Number of successfully uploaded file
								}
							}
						}
					}
				}
				if($_FILES["strRestoScreenShots"]["tmp_name"]!='')
				{
					$path = "assets/uploaded/screenshots/"; // Upload directory
					$count = 0;
					
					if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
						// Loop $_FILES to execute all files
						foreach ($_FILES['strRestoScreenShots']['name'] as $f => $name) {     
							if ($_FILES['strRestoScreenShots']['error'][$f] == 4) {
								continue; // Skip file if any error found
							}	       
							if ($_FILES['strRestoScreenShots']['error'][$f] == 0) {	           
								if ($_FILES['strRestoScreenShots']['size'][$f] > $max_file_size) {
									$message[] = "$name is too large!.";
									continue; // Skip large files
								}
								elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
									$message[] = "$name is not a valid format";
									continue; // Skip invalid file formats
								}
								else{ // No error found! Move uploaded files 
									$fileName = $randNumber.'_'.$name; 
									if(move_uploaded_file($_FILES["strRestoScreenShots"]["tmp_name"][$f], $path.$fileName))
									$intRestoID = stripslashes(filter_var($post['intRestoOwer'], FILTER_SANITIZE_STRING));
									$strRestoAsset = $fileName;
									$this->restaurant_class->save_resto_assets($intRestoID, $strRestoAsset);
									$count++; // Number of successfully uploaded file
								}
							}
						}
					}
				}
			}
		}
		
		return $saveRestoDetails;
	}
	public function add_branch_owner($post)
	{
		$restoOwner = $this->users_class->save_branch_owner($post);
		return $restoOwner;	
	}
	public function add_branch($mainBranchID)
	{
		$data = $this->global_data();
		
		$data['mainBranchID'] = $mainBranchID;
		
		$data['isRestoOwner'] = $this->restaurant_class->is_resto_owner($mainBranchID);
		
		$post = $this->input->post();
		if(isset($post['doAddOwner']))
		{
			if($post['doAddOwner']=='add_owner')
			{
				$saveOwnerDetails = $this->add_branch_owner($post);
				if($saveOwnerDetails)
				{
					$data['intStatus'] = 1;
				}
				else
				{
					$data['intStatus'] = 0;
				}
			}
		}
		else
		{
			$data['intStatus'] = '';
		}
		if(isset($post['doAddResto']))
		{
			if($post['doAddResto']=='add_resto')
			{
				$saveRestoDetails = $this->save_branch_details();
				if($saveRestoDetails['picMsg'] == 'err_thumbnail')
				{
					$data['picMsg'] = 'err_thumbnail';
				}
				else if($saveRestoDetails['picMsg'] == 'err_banner')
				{
					$data['picMsg'] = 'err_banner';
				}
				else if($saveRestoDetails)
				{
					$data['intStatus'] = 1;
					$data['picMsg'] = '';
				}
				else
				{
					$data['intStatus'] = 0;
					$data['picMsg'] = '';
				}
			}
		}
		else
		{
			$data['intStatus'] = '';
			$data['picMsg'] = '';
		}
		$data['mainBranchExist'] = $this->restaurant_class->main_branch_exist();
		$data['isBranchOwner'] = $this->restaurant_class->is_branch_owner();
		
		$this->load->view('commons/header', $data);
		$this->load->view('add_branch/commons/css');
		$this->load->view('add_branch/index');
		$this->load->view('commons/js');
		$this->load->view('add_branch/commons/js');
		$this->load->view('commons/footer');
	}
	public function save_branch_details()
	{
		$post = $this->input->post();

		$imgRestoThumbSize = getimagesize($_FILES['strRestoThumbnail']['tmp_name'][0]);
		$imgRestoBannerSize = getimagesize($_FILES['strRestoBanner']['tmp_name'][0]);

		if($imgRestoThumbSize[0]!='189' || $imgRestoThumbSize[1]!='86')
		{
			$saveRestoDetails['picMsg'] = 'err_thumbnail';
		}
		else if($imgRestoBannerSize[0]!='1098' || $imgRestoBannerSize[1]!='469')
		{
			$saveRestoDetails['picMsg'] = 'err_banner';
		}
		else
		{
			$saveRestoDetails = $this->restaurant_class->save_branch_details($post);
			
			if($saveRestoDetails['intStatus']=='1')
			{
				$valid_formats = array("jpg", "jpeg", "png");
				$max_file_size = 1024*20480; //20 MB
				$randNumber = rand('111111','999999');
				
				if($_FILES["strRestoThumbnail"]["tmp_name"]!='')
				{
					$path = "assets/uploaded/thumbnails/"; // Upload directory
					$count = 0;
					
					if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
						// Loop $_FILES to execute all files
						foreach ($_FILES['strRestoThumbnail']['name'] as $f => $name) {     
							if ($_FILES['strRestoThumbnail']['error'][$f] == 4) {
								continue; // Skip file if any error found
							}	       
							if ($_FILES['strRestoThumbnail']['error'][$f] == 0) {	           
								if ($_FILES['strRestoThumbnail']['size'][$f] > $max_file_size) {
									$message[] = "$name is too large!.";
									continue; // Skip large files
								}
								elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
									$message[] = "$name is not a valid format";
									continue; // Skip invalid file formats
								}
								else{ // No error found! Move uploaded files 
									$fileName = $randNumber.'_'.$name; 
									if(move_uploaded_file($_FILES["strRestoThumbnail"]["tmp_name"][$f], $path.$fileName))
									$intRestoID = $this->session->userdata('intRestoID');
									$strRestoThumb = $fileName;
									$this->restaurant_class->save_resto_thumb($intRestoID, $strRestoThumb);
									$count++; // Number of successfully uploaded file
								}
							}
						}
					}
				}
				if($_FILES["strRestoBanner"]["tmp_name"]!='')
				{
					$path = "assets/uploaded/banners/"; // Upload directory
					$count = 0;
					
					if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
						// Loop $_FILES to execute all files
						foreach ($_FILES['strRestoBanner']['name'] as $f => $name) {     
							if ($_FILES['strRestoBanner']['error'][$f] == 4) {
								continue; // Skip file if any error found
							}	       
							if ($_FILES['strRestoBanner']['error'][$f] == 0) {	           
								if ($_FILES['strRestoBanner']['size'][$f] > $max_file_size) {
									$message[] = "$name is too large!.";
									continue; // Skip large files
								}
								elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
									$message[] = "$name is not a valid format";
									continue; // Skip invalid file formats
								}
								else{ // No error found! Move uploaded files 
									$fileName = $randNumber.'_'.$name; 
									if(move_uploaded_file($_FILES["strRestoBanner"]["tmp_name"][$f], $path.$fileName))
									$intRestoID = $this->session->userdata('intRestoID');
									$strRestoBanner = $fileName;
									$this->restaurant_class->save_resto_banner($intRestoID, $strRestoBanner);
									$count++; // Number of successfully uploaded file
								}
							}
						}
					}
				}
				if($_FILES["strRestoScreenShots"]["tmp_name"]!='')
				{
					$path = "assets/uploaded/screenshots/"; // Upload directory
					$count = 0;
					
					if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
						// Loop $_FILES to execute all files
						foreach ($_FILES['strRestoScreenShots']['name'] as $f => $name) {     
							if ($_FILES['strRestoScreenShots']['error'][$f] == 4) {
								continue; // Skip file if any error found
							}	       
							if ($_FILES['strRestoScreenShots']['error'][$f] == 0) {	           
								if ($_FILES['strRestoScreenShots']['size'][$f] > $max_file_size) {
									$message[] = "$name is too large!.";
									continue; // Skip large files
								}
								elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
									$message[] = "$name is not a valid format";
									continue; // Skip invalid file formats
								}
								else{ // No error found! Move uploaded files 
									$fileName = $randNumber.'_'.$name; 
									if(move_uploaded_file($_FILES["strRestoScreenShots"]["tmp_name"][$f], $path.$fileName))
									$intRestoID = $this->session->userdata('intRestoID');
									$strRestoAsset = $fileName;
									$this->restaurant_class->save_resto_assets($intRestoID, $strRestoAsset);
									$count++; // Number of successfully uploaded file
								}
							}
						}
					}
				}
			}
		}
		
		return $saveRestoDetails;
	}
	public function edit_restaurant($intRestoID=null)
	{
		$data = $this->global_data();
		
		$data['intRestoID'] = $intRestoID;
		
		$post = $this->input->post();
		if(isset($post['doUpdateResto']))
		{
			if($post['doUpdateResto']=='update_resto')
			{
				$saveRestoDetails = $this->update_branch_details();
				if($saveRestoDetails['picMsg'] == 'err_thumbnail')
				{
					$data['picMsg'] = 'err_thumbnail';
				}
				else if($saveRestoDetails['picMsg'] == 'err_banner')
				{
					$data['picMsg'] = 'err_banner';
				} 
				else if($saveRestoDetails)
				{
					$data['intStatus'] = 1;
					$data['picMsg'] = '';
				}
				else
				{
					$data['intStatus'] = 0;
					$data['picMsg'] = '';
				}
			}
		}
		else
		{
			$data['intStatus'] = '';
			$data['picMsg'] = '';
		}
		$data['isRestoExist'] = $this->restaurant_class->is_resto_exist($intRestoID);
		$data['theRestoDetails'] = $this->restaurant_class->the_resto_details($intRestoID);
		$data['theRestoAssets'] = $this->restaurant_class->list_resto_assets($intRestoID);
		
		$this->load->view('commons/header', $data);
		$this->load->view('edit_restaurant/commons/css');
		$this->load->view('edit_restaurant/index');
		$this->load->view('commons/js');
		$this->load->view('edit_restaurant/commons/js');
		$this->load->view('commons/footer');
	}
	public function update_branch_details()
	{
		$post = $this->input->post();
		
		//if($_FILES["strRestoThumbnail"]["tmp_name"][0]!='')
		//{
		//	$imgRestoThumbSize = getimagesize($_FILES['strRestoThumbnail']['tmp_name'][0]);
		//}
		//else
		//{
		//	$saveRestoDetails = $this->restaurant_class->update_branch_details($post);
		//	$imgRestoThumbSize = array('0'=>'');
		//}
		//if($_FILES["strRestoBanner"]["tmp_name"][0]!='')
		//{
		//	$imgRestoBannerSize = getimagesize($_FILES['strRestoBanner']['tmp_name'][0]);
		//}
		//else
		//{
		//	$saveRestoDetails = $this->restaurant_class->update_branch_details($post);
		//	$imgRestoBannerSize = array('0'=>'');
		//}

		//if($imgRestoThumbSize[0]!='189' || $imgRestoThumbSize[1]!='86')
		//{
		//	$saveRestoDetails['picMsg'] = 'err_thumbnail';
		//}
		//else if($imgRestoBannerSize[0]!='1098' || $imgRestoBannerSize[1]!='469')
		//{
		//	$saveRestoDetails['picMsg'] = 'err_banner';
		//}
		//else
		//{
			$saveRestoDetails = $this->restaurant_class->update_branch_details($post);
			
			if($saveRestoDetails['intStatus']=='1')
			{
				$valid_formats = array("jpg", "jpeg", "png");
				$max_file_size = 1024*20480; //20 MB
				$randNumber = rand('111111','999999');
				
				if($_FILES["strRestoThumbnail"]["tmp_name"]!='')
				{
					$path = "assets/uploaded/thumbnails/"; // Upload directory
					$count = 0;
					
					if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
						// Loop $_FILES to execute all files
						foreach ($_FILES['strRestoThumbnail']['name'] as $f => $name) {     
							if ($_FILES['strRestoThumbnail']['error'][$f] == 4) {
								continue; // Skip file if any error found
							}	       
							if ($_FILES['strRestoThumbnail']['error'][$f] == 0) {	           
								if ($_FILES['strRestoThumbnail']['size'][$f] > $max_file_size) {
									$message[] = "$name is too large!.";
									continue; // Skip large files
								}
								elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
									$message[] = "$name is not a valid format";
									continue; // Skip invalid file formats
								}
								else{ // No error found! Move uploaded files 
									$fileName = $randNumber.'_'.$name; 
									if(move_uploaded_file($_FILES["strRestoThumbnail"]["tmp_name"][$f], $path.$fileName))
									$intRestoID = $post['intRestoID'];
									$strRestoThumb = $fileName;
									$this->restaurant_class->update_resto_thumb($intRestoID, $strRestoThumb);
									$count++; // Number of successfully uploaded file
								}
							}
						}
					}
				}
				if($_FILES["strRestoBanner"]["tmp_name"]!='')
				{
					$path = "assets/uploaded/banners/"; // Upload directory
					$count = 0;
					
					if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
						// Loop $_FILES to execute all files
						foreach ($_FILES['strRestoBanner']['name'] as $f => $name) {     
							if ($_FILES['strRestoBanner']['error'][$f] == 4) {
								continue; // Skip file if any error found
							}	       
							if ($_FILES['strRestoBanner']['error'][$f] == 0) {	           
								if ($_FILES['strRestoBanner']['size'][$f] > $max_file_size) {
									$message[] = "$name is too large!.";
									continue; // Skip large files
								}
								elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
									$message[] = "$name is not a valid format";
									continue; // Skip invalid file formats
								}
								else{ // No error found! Move uploaded files 
									$fileName = $randNumber.'_'.$name; 
									if(move_uploaded_file($_FILES["strRestoBanner"]["tmp_name"][$f], $path.$fileName))
									$intRestoID = $post['intRestoID'];
									$strRestoBanner = $fileName;
									$this->restaurant_class->update_resto_banner($intRestoID, $strRestoBanner);
									$count++; // Number of successfully uploaded file
								}
							}
						}
					}
				}
				/*if($_FILES["strRestoScreenShots"]["tmp_name"]!='')
				{
					$path = "assets/uploaded/screenshots/"; // Upload directory
					$count = 0;
					
					if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
						// Loop $_FILES to execute all files
						foreach ($_FILES['strRestoScreenShots']['name'] as $f => $name) {     
							if ($_FILES['strRestoScreenShots']['error'][$f] == 4) {
								continue; // Skip file if any error found
							}	       
							if ($_FILES['strRestoScreenShots']['error'][$f] == 0) {	           
								if ($_FILES['strRestoScreenShots']['size'][$f] > $max_file_size) {
									$message[] = "$name is too large!.";
									continue; // Skip large files
								}
								elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
									$message[] = "$name is not a valid format";
									continue; // Skip invalid file formats
								}
								else{ // No error found! Move uploaded files 
									$fileName = $randNumber.'_'.$name; 
									if(move_uploaded_file($_FILES["strRestoScreenShots"]["tmp_name"][$f], $path.$fileName))
									$intRestoID = $post['intRestoID'];
									$strRestoAsset = $fileName;
									$this->restaurant_class->update_resto_assets($intRestoID, $strRestoAsset);
									$count++; // Number of successfully uploaded file
								}
							}
						}
					}
				}*/
			}
		//}
		
		return $saveRestoDetails;
	}
	public function view_branches($id=null)
	{
		$data = $this->global_data();
		
		$page = $this->input->get('next/');

		if (!$page) {
			$offset = 0;
		} else {
			$offset = $page;
		}
		$data['listBranches'] = $this->restaurant_class->view_branches($id.'/next', $offset, $this->commons->MAX_RESULTS);
		$totalPage = $this->restaurant_class->get_view_branches_total($id);
		
		$pageConfig = $this->commons->createPagingConfig(base_url().'dashboard/view_branches/'.$id.'/next', $totalPage, $this->commons->MAX_RESULTS);
		$this->pagination->initialize($pageConfig);
		$pageLinks = $this->pagination->create_links();
		
		$data['totalPage'] = $totalPage;
		$data['pageLinks'] = $pageLinks;

		$this->load->view('commons/header', $data);
		$this->load->view('view_branches/commons/css');
		$this->load->view('view_branches/index');
		$this->load->view('commons/js');
		$this->load->view('view_branches/commons/js');
		$this->load->view('commons/footer');
	}
		public function view_branches_account($id=null)
	{
			$data = $this->global_data();
		
		$page = $this->input->get('next/');

		if (!$page) { 
			$offset = 0;
		} else {
			$offset = $page;
		}
		$data['listBranches'] = $this->restaurant_class->view_branches($id.'/next', $offset, $this->commons->MAX_RESULTS);
		$data['getMainBranch'] = $this->restaurant_class->get_main_branch_name($id);
		$data['getOwner'] = $this->restaurant_class->get_main_branch_owner($id);
		$totalPage = $this->restaurant_class->get_view_branches_total($id);
		
		$pageConfig = $this->commons->createPagingConfig(base_url().'dashboard/view_branches_account/'.$id.'/next', $totalPage, $this->commons->MAX_RESULTS);
		$this->pagination->initialize($pageConfig);
		$pageLinks = $this->pagination->create_links();
		
		$data['totalPage'] = $totalPage;
		$data['pageLinks'] = $pageLinks;

		$this->load->view('commons/header', $data);
		$this->load->view('view_branches/commons/css');
		$this->load->view('view_branches/viewbranches');
		$this->load->view('commons/js');
		$this->load->view('view_branches/commons/js');
		$this->load->view('commons/footer');
	}
	public function delete_resto()
	{
		$post = $this->input->post();
		
		$deleteResto = $this->restaurant_class->delete_resto($post);
		
		echo json_encode($deleteResto);
	}
	public function logout()
	{
		$this->session->unset_userdata('user_logged_in');
		$this->session->unset_userdata('intUserID');
		$this->session->unset_userdata('strEmail');
		$this->session->unset_userdata('intAccountType');
		$this->session->sess_destroy();
		redirect(base_url().'home','refresh');	
	}
	public function databases()
	{
		$data = $this->global_data();
		
		$this->load->view('commons/header', $data);
		$this->load->view('dashboard/commons/css');
		$this->load->view('dashboard/index');
		$this->load->view('commons/js');
		$this->load->view('dashboard/commons/js');
		$this->load->view('commons/footer');
	}
	public function accounts_analysis($page=null)
	{
$data = $this->global_data();
		
		if (!$page) {
			$offset = 0;
		} else {
			$offset = $page;
		}

		$data['listMainBranch'] = $this->restaurant_class->list_main_branch($offset, $this->commons->MAX_RESULTS);
		$totalPage = $this->restaurant_class->list_main_branch_total();
		
		$pageConfig = $this->commons->createPagingConfig(base_url().'dashboard/accounts_analysis', $totalPage, $this->commons->MAX_RESULTS);
		$this->pagination->initialize($pageConfig);
		$pageLinks = $this->pagination->create_links();
		
		$data['totalPage'] = $totalPage;
		$data['pageLinks'] = $pageLinks;
		
		$this->load->view('commons/header', $data);
		$this->load->view('accounts_analysis/commons/css');
		$this->load->view('accounts_analysis/index');
		$this->load->view('commons/js');
		$this->load->view('accounts_analysis/commons/js');
		$this->load->view('commons/footer');
	}
	public function account_analysis_assets()
	{
		$post = $this->input->post();
		
		$data['listAssets'] = $this->restaurant_class->account_analysis_assets($post);
				$this->load->view('accounts_analysis/assets',$data);

		}
		
			public function reservation_confirm()
	{
	
		$post = $this->input->post();
		
		$updateUser = $this->reservation_class->confirm($post);

			echo $updateUser['confirm'];
		}
					public function reservation_deny()
	{
		$post = $this->input->post();
		
		$updateUser = $this->reservation_class->deny($post);

			echo $updateUser['confirm'];
		}
		public function account_summary($page=null)
		{
		
		$data = $this->global_data();
			if (!$page) {
			$offset = 0;
		} else {
			$offset = $page;
		}
		// **weekly tab *** //
		if($page == 'weekly')
		{
		$data['tabb'] = 'weekly';
		
		$data['summary_today'] = $this->restaurant_class->account_summary_weekly($offset, $this->commons->MAX_RESULTS,$data);
		$totalPage = $this->restaurant_class->account_summary_total_weekly();
		
		$pageConfig = $this->commons->createPagingConfig(base_url().'dashboard/account_summary/weekly', $totalPage, $this->commons->MAX_RESULTS);
		$this->pagination->initialize($pageConfig);
		$pageLinks = $this->pagination->create_links();
		
		$data['totalPage'] = $totalPage;
		$data['pageLinks'] = $pageLinks;		
		}
		
		if($page == ''){
	// **today tab** //
		$data['tabb'] = '';
		$data['summary_today'] = $this->restaurant_class->account_summary($offset, $this->commons->MAX_RESULTS,$data);
		$totalPage = $this->restaurant_class->account_summary_total();
		
		$pageConfig = $this->commons->createPagingConfig(base_url().'dashboard/account_summary', $totalPage, $this->commons->MAX_RESULTS);
		$this->pagination->initialize($pageConfig);
		$pageLinks = $this->pagination->create_links();
		
		$data['totalPage'] = $totalPage;
		$data['pageLinks'] = $pageLinks;
		}
				
		if($page == 'monthly'){
	// **today tab** //
		$data['tabb'] = 'monthly';
		$data['summary_today'] = $this->restaurant_class->account_summary_monthly($offset, $this->commons->MAX_RESULTS,$data);
		$totalPage = $this->restaurant_class->account_summary_total_monthly();
		
		$pageConfig = $this->commons->createPagingConfig(base_url().'dashboard/account_summary', $totalPage, $this->commons->MAX_RESULTS);
		$this->pagination->initialize($pageConfig);
		$pageLinks = $this->pagination->create_links();
		
		$data['totalPage'] = $totalPage;
		$data['pageLinks'] = $pageLinks;
		}
		$this->load->view('commons/header', $data);
		$this->load->view('accounts_summary/commons/css');
		$this->load->view('accounts_summary/includes/header');
		$this->load->view('accounts_summary/index');
		$this->load->view('accounts_summary/includes/footer');
		$this->load->view('commons/js');
		$this->load->view('accounts_summary/commons/js');
		$this->load->view('commons/footer');

		}
		public function list_users($page=null)
		{
				$data = $this->global_data();
			if (!$page) {
			$offset = 0;
		} else {
			$offset = $page;
		}	
		
		$data['userlist'] = $this->users_class->user_lists($offset, $this->commons->MAX_RESULTS);
		$totalPage = $this->users_class->get_user_data_total();
		
		$pageConfig = $this->commons->createPagingConfig(base_url().'dashboard/list_users', $totalPage, $this->commons->MAX_RESULTS);
		$this->pagination->initialize($pageConfig);
		$pageLinks = $this->pagination->create_links();
		
		$data['totalPage'] = $totalPage;
		$data['pageLinks'] = $pageLinks;
		
		$this->load->view('commons/header', $data);
		$this->load->view('list_users/commons/css');
		$this->load->view('list_users/includes/header');
		$this->load->view('list_users/index');
		$this->load->view('list_users/includes/footer');
		$this->load->view('commons/js');
		$this->load->view('list_users/commons/js');
		$this->load->view('commons/footer');
			
		}
		public function reserved_lists($page=null)
		{
		$data = $this->global_data();
		if (!$page) {
			$offset = 0;
		} else {
			$offset = $page;
		}	
		
		$data['reslist'] = $this->users_class->reserved_lists($offset, $this->commons->MAX_RESULTS);
		$totalPage = $this->users_class->reserved_lists_total();
		
		$pageConfig = $this->commons->createPagingConfig(base_url().'dashboard/list_users', $totalPage, $this->commons->MAX_RESULTS);
		$this->pagination->initialize($pageConfig);
		$pageLinks = $this->pagination->create_links();
		
		$data['totalPage'] = $totalPage;
		$data['pageLinks'] = $pageLinks;
		
		$this->load->view('commons/header',$data);
		$this->load->view('reserved_lists/commons/css');
		$this->load->view('reserved_lists/includes/header');
		$this->load->view('reserved_lists/index');
		$this->load->view('reserved_lists/includes/footer');
		$this->load->view('commons/js');
		$this->load->view('reserved_lists/commons/js');
		$this->load->view('commons/footer');
		}
		
}